package org.linlinjava.litemall.wx.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CuoDeviceClient {
    // 获取Token
    public static String getToken(String appId, String appSecurity) {
        JSONObject params = JSONUtil.createObj()
                .set("appId", appId)
                .set("appSecurity", appSecurity);

        HttpResponse response = HttpRequest.post("http://112.15.190.123:9113/auth/token")
                .header("Content-Type", "application/json")
                .body(params.toString())
                .execute();
        System.out.println(response.body());
        JSONObject result = JSONUtil.parseObj(response.body());
        return result.getStr("data");
    }

    // 获取设备列表
    public static JSONObject getDeviceList(String token, String deviceName,
                                           String deviceIdentification) {
        HttpRequest request = HttpRequest.get("http://112.15.190.123:9113/cuodevice/list")
                .header("Authorization", token);

        // 添加查询参数
        Map<String, Object> paramMap = new HashMap<>();
        if (deviceName != null) paramMap.put("deviceName", deviceName);
        if (deviceIdentification != null) paramMap.put("deviceIdentification", deviceIdentification);

        if (!paramMap.isEmpty()) {
            request.form(paramMap);
        }

        HttpResponse response = request.execute();
        return JSONUtil.parseObj(response.body());
    }

    public static JSONObject getCuoDeviceData(String token,String body) {
        HttpRequest request = HttpRequest.get("http://112.15.190.123:9113/cuodevicedata/list").method(Method.POST)
                .header("Authorization", token);

        request.body(body);
        HttpResponse response = request.execute();
        return JSONUtil.parseObj(response.body());
    }


    // 发送控制指令
    public static boolean sendCommand(String token, String deviceId,
                                      List<Map<String, String>> cmdParams) {
        JSONObject params = JSONUtil.createObj()
                .set("deviceIdentification", deviceId)
                .set("cmdParams", cmdParams);

        HttpResponse response = HttpRequest.post("http://112.15.190.123:9113/cuodevice/cmd/setparam")
                .header("Authorization", token)
                .header("Content-Type", "application/json")
                .body(params.toString())
                .execute();

        JSONObject result = JSONUtil.parseObj(response.body());
        return result.getInt("code") == 200;
    }

    // 其他方法类似结构...

    public static void main(String[] args) {
        // 使用示例
        String token = getToken("test_app_1", "Fuli_iotlinks_2025");

        System.out.println("token：" + token);

        // 获取设备列表
        JSONObject devices = getDeviceList(token, null, null);
        System.out.println(devices);

        // 发送指令示例
        List<Map<String, String>> cmdParams = new ArrayList<>();
        Map<String, String> param1 = new HashMap<>();
        param1.put("param_name", "deviceMode");
        param1.put("param_value", "3");
        cmdParams.add(param1);

        boolean success = sendCommand(token, "fuli_cuo_001", cmdParams);
        System.out.println("指令发送结果：" + success);
    }
}
